home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / size / size.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-16  |  3.1 KB  |  129 lines

  1. /*
  2.  * size.c --
  3.  *
  4.  *    This file contains the "size" command, which prints out
  5.  *    the sizes of the segments of one or more object files.
  6.  *
  7.  *    Adding a new machine type: Write a routine that prints out the
  8.  *    desired information if it recognizes the exec header. Look
  9.  *    at one of the existing routines for the parameter specifications. 
  10.  *      This routine should return SUCCESS if it printed out the size, 
  11.  *    and FAILURE otherwise. Add the name of this routine to the 
  12.  *    printProc array.
  13.  *
  14.  *
  15.  * Copyright 1988 Regents of the University of California
  16.  * Permission to use, copy, modify, and distribute this
  17.  * software and its documentation for any purpose and without
  18.  * fee is hereby granted, provided that the above copyright
  19.  * notice appear in all copies.  The University of California
  20.  * makes no representations about the suitability of this
  21.  * software for any purpose.  It is provided "as is" without
  22.  * express or implied warranty.
  23.  */
  24.  
  25. #ifndef lint
  26. static char rcsid[] = "$Header: /sprite/src/cmds/size/RCS/size.c,v 1.7 90/02/16 13:46:45 rab Exp $";
  27. #endif /* not lint */
  28.  
  29. #include <errno.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <sys/file.h>
  33. #include "size.h"
  34.  
  35. /*
  36.  * Array of routines to print out size.
  37.  */
  38. ReturnStatus (*(printProc[])) () = {
  39.     Print68k,   /* This should be `PrintSun' since it works for sparc too. */
  40.     PrintSpur,
  41.     PrintMips,
  42. };
  43.  
  44. #define MACHINECOUNT (sizeof (printProc) / sizeof(*printProc))
  45.  
  46. int hostFmt = HOST_FMT;
  47.  
  48. /*
  49.  *----------------------------------------------------------------------
  50.  *
  51.  * main --
  52.  *
  53.  *    Main program for "size".
  54.  *
  55.  * Results:
  56.  *    None.
  57.  *
  58.  * Side effects:
  59.  *    Prints info on standard output.
  60.  *
  61.  *----------------------------------------------------------------------
  62.  */
  63.  
  64. main(argc, argv)
  65.     int argc;
  66.     char *argv[];
  67. {
  68.     int         i;
  69.     int            j;
  70.     Boolean         printName;
  71.     char        *fileName;
  72.     int            filesToDo;
  73.     int            targetType;
  74.     int            lastType;
  75.     char        headerBuffer[HEADERSIZE];
  76.     int            amountRead;
  77.     FILE        *fp;
  78.     ReturnStatus    status;
  79.  
  80.     lastType = -1;
  81.     if (argc < 2) {
  82.     filesToDo = 1;
  83.     fileName = "a.out";
  84.     printName = FALSE;
  85.     } else if (argc == 2) {
  86.     filesToDo = 1;
  87.     fileName = NULL;
  88.     printName = FALSE;
  89.     } else {
  90.     filesToDo = argc - 1;
  91.     fileName = NULL;
  92.     printName = TRUE;
  93.     }
  94.     for (i = 0; i < filesToDo; i++,fileName = NULL) {
  95.     if (fileName == NULL) {
  96.         fileName = argv[i+1];
  97.     }
  98.     fp = fopen(fileName, "r");
  99.     if (fp == NULL) {
  100.         fprintf(stderr, "Couldn't open \"%s\": %s.\n",
  101.             fileName, strerror(errno));
  102.         exit(1);
  103.     }
  104.     amountRead = fread(headerBuffer, sizeof(char), HEADERSIZE, fp);
  105.     if (amountRead < 0) {
  106.         fprintf(stderr, "Couldn't read header for \"%s\": %s.\n",
  107.             fileName, strerror(errno));
  108.         exit(1);
  109.     }
  110.     status = FAILURE;
  111.     rewind(fp);
  112.     for (j = 0; j < MACHINECOUNT; j++) {
  113.         status = printProc[j](fp, printName, fileName, 
  114.                  (j == lastType) ? FALSE : TRUE,
  115.                  amountRead, headerBuffer);
  116.         if (status == SUCCESS) {
  117.         lastType = j;
  118.         break;
  119.         }
  120.     }
  121.     if (status == FAILURE) {
  122.         fprintf(stderr, 
  123.           "\"%s\" isn't an object file for any known machine.\n",
  124.             fileName);
  125.     }
  126.     }
  127.     exit(0);
  128. }
  129.